package ms.authority.controller;

import com.alibaba.fastjson.JSONObject;
import ms.authority.bean.UserParamBean;
import ms.authority.bean.UserQueryParamBean;
import ms.authority.config.ResponseCode;
import ms.authority.entity.User;
import ms.authority.service.UserService;
import ms.common.exception.BizException;
import ms.common.util.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import page.Page;

/**
 * Project Name: itye
 * File Name: UserController
 * Package Name: ms.authority.controller
 * Date: 2018/5/3  14:26
 * Copyright (c) 2018, tianyul All Rights Reserved.
 * 管理员管理
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询数据
     * @param userQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<User> query(@RequestBody UserQueryParamBean userQueryParamBean) {
        if(null == userQueryParamBean.getPageNo() || userQueryParamBean.getPageNo() < 1) {
            userQueryParamBean.setPageNo(1);
        }
        if(null == userQueryParamBean.getPageSize() || userQueryParamBean.getPageSize() < 1) {
            userQueryParamBean.setPageSize(10);
        }
        if(StringUtil.isEmpty(userQueryParamBean.getRealName())) {
            userQueryParamBean.setRealName("");
        }
        String key = JSONObject.toJSONString(userQueryParamBean.getRealName()+(StringUtil.isEmpty(userQueryParamBean.getStatus())?"":userQueryParamBean.getStatus()));
        if("\"\"".equals(key)) {
            key = "";
        }
        return userService.queryPage(userQueryParamBean, key);
    }

    /**
     * 新增
     * @param userParamBean
     * @return
     */
    @PostMapping("/create")
    public void create(@RequestBody UserParamBean userParamBean) {
        checkParam(userParamBean);
        userService.create(userParamBean);
    }

    /**
     * 修改
     * @param userParamBean
     * @return
     */
    @PostMapping("/update")
    public void update(@RequestBody UserParamBean userParamBean) {
        checkParam(userParamBean);
        userService.update(userParamBean);
    }

    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") String id) {
        userService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") String id) {
        userService.cancel(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public User detail(@PathVariable("id") String id) {
        return userService.detail(id);
    }

    /**
     * 校验新增和修改参数
     * @param userParamBean
     */
    private void checkParam(UserParamBean userParamBean) {
        if(StringUtil.isEmpty(userParamBean.getRealName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户名称不能为空");
        }
        if(StringUtil.isEmpty(userParamBean.getUserName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户账号不能为空");
        }
        if(StringUtil.isEmpty(userParamBean.getPwd())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "密码不能为空");
        }
        if(StringUtil.isEmpty(userParamBean.getPermissions())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "权限不能为空");
        }
    }

}
